//
//  YJProgressHUD.m
//  PictureHouseKeeper
//
//  Created by 李亚军 on 16/8/19.
//  Copyright © 2016年 zyyj. All rights reserved.
//

#import "YJProgressHUD.h"


@implementation YJProgressHUD

+(instancetype)shareinstance{
    
    static YJProgressHUD *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[YJProgressHUD alloc] init];
    });
    
    return instance;
}
+(void)show:(NSString *)msg inView:(UIView *)view mode:(YJProgressMode)myMode{
        //如果已有弹框，先消失
        if ([YJProgressHUD shareinstance].hud != nil) {
            [[YJProgressHUD shareinstance].hud hideAnimated:YES];
            [YJProgressHUD shareinstance].hud = nil;
        }
        
        //4\4s屏幕避免键盘存在时遮挡
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            [view endEditing:YES];
        }
        
        [YJProgressHUD shareinstance].hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        //    [YJProgressHUD shareinstance].hud.dimBackground = YES;    //是否显示透明背景
        [YJProgressHUD shareinstance].hud.color = [UIColor blackColor];
        [[YJProgressHUD shareinstance].hud setMargin:10];
        [[YJProgressHUD shareinstance].hud setRemoveFromSuperViewOnHide:YES];
        [YJProgressHUD shareinstance].hud.detailsLabel.text = msg;
        [YJProgressHUD shareinstance].hud.contentColor = [UIColor whiteColor];
        [YJProgressHUD shareinstance].hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        switch ((NSInteger)myMode) {
            case YJProgressModeOnlyText:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeText;
                break;
                
            case YJProgressModeLoading:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeIndeterminate;
                break;
                
            case YJProgressModeCircleLoading:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeDeterminate;
                break;
                
            case YJProgressModeSuccess:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeCustomView;
                [YJProgressHUD shareinstance].hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success"]];
                break;
                
            default:
                break;
        }
    
}

+(void)zj_MainThead_show:(NSString *)msg inView:(UIView *)view mode:(YJProgressMode)myMode{
    dispatch_async(dispatch_get_main_queue(), ^{
        //如果已有弹框，先消失
        if ([YJProgressHUD shareinstance].hud != nil) {
            [[YJProgressHUD shareinstance].hud hideAnimated:YES];
            [YJProgressHUD shareinstance].hud = nil;
        }
        
        //4\4s屏幕避免键盘存在时遮挡
        if ([UIScreen mainScreen].bounds.size.height == 480) {
            [view endEditing:YES];
        }
        
        [YJProgressHUD shareinstance].hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        //    [YJProgressHUD shareinstance].hud.dimBackground = YES;    //是否显示透明背景
        [YJProgressHUD shareinstance].hud.color = [UIColor blackColor];
        [[YJProgressHUD shareinstance].hud setMargin:10];
        [[YJProgressHUD shareinstance].hud setRemoveFromSuperViewOnHide:YES];
        [YJProgressHUD shareinstance].hud.detailsLabel.text = msg;
        [YJProgressHUD shareinstance].hud.contentColor = [UIColor whiteColor];
        [YJProgressHUD shareinstance].hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        switch ((NSInteger)myMode) {
            case YJProgressModeOnlyText:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeText;
                break;
                
            case YJProgressModeLoading:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeIndeterminate;
                break;
                
            case YJProgressModeCircleLoading:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeDeterminate;
                break;
                
            case YJProgressModeSuccess:
                [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeCustomView;
                [YJProgressHUD shareinstance].hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success"]];
                break;
                
            default:
                break;
        }
        
        
    });
}
+ (void)showMessage:(NSString *)msg duration:(CGFloat)time view:(UIView *)view{
    [self show:msg inView:view mode:YJProgressModeLoading];
}
//+ (void)showMessage:(NSString *)msg duration:(CGFloat)time view:(UIView *)view
//{
//
//    //如果已有弹框，先消失
//    if ([YJProgressHUD shareinstance].hud != nil) {
//        [[YJProgressHUD shareinstance].hud hideAnimated:YES];
//        [YJProgressHUD shareinstance].hud = nil;
//    }
//
//    [YJProgressHUD shareinstance].hud =[MBProgressHUD showHUDAddedTo:view animated:YES];
//
//    // 显示模式,改成customView,即显示自定义图片(mode设置,必须写在customView赋值之前)
//    [YJProgressHUD shareinstance].hud.mode = MBProgressHUDModeCustomView;
//
//    // 设置要显示 的自定义的图片
//    UIImageView *imageView = [[UIImageView alloc] init];
//    NSMutableArray *array = [NSMutableArray array];
//    for (int i = 1; i < 27 ; i ++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"N%d",i]];
//        [array addObject:image];
//    }
//
//    imageView.animationImages = array;
//    imageView.animationDuration = 1;
//    [imageView startAnimating];
//    [YJProgressHUD shareinstance].hud.customView = imageView;
//    // 显示的文字,比如:加载失败...加载中...
//    [YJProgressHUD shareinstance].hud.label.text = msg;
//    // 标志:必须为YES,才可以隐藏,  隐藏的时候从父控件中移除
//    [YJProgressHUD shareinstance].hud.removeFromSuperViewOnHide = YES;
//    [YJProgressHUD shareinstance].hud.margin = 10.0f;
//    [YJProgressHUD shareinstance].hud.offset = CGPointMake(0, LP_NavViewHeight); //距离屏幕边缘的距离
//    [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:time];
//    [YJProgressHUD shareinstance].hud.center = view.center;
//}


+(void)zj_MainThead_hide{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([YJProgressHUD shareinstance].hud != nil) {
            [[YJProgressHUD shareinstance].hud hideAnimated:YES];
        }
    });
}

+(void)hide{
    if ([YJProgressHUD shareinstance].hud != nil) {
        [[YJProgressHUD shareinstance].hud hideAnimated:YES];
    }

}


+(void)showMessage:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:YJProgressModeOnlyText];
    [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
}
+(void)zj_MainThead_showMessage:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:YJProgressModeOnlyText];
    dispatch_async(dispatch_get_main_queue(), ^{
        [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    });
}

+(void)showOnlyText:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:YJProgressModeOnlyText];
}

+(void)showMessage:(NSString *)msg inView:(UIView *)view afterDelayTime:(NSInteger)delay{
    [self show:msg inView:view mode:YJProgressModeOnlyText];
    [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:delay];
}

+(void)showSuccess:(NSString *)msg inview:(UIView *)view{
    [self show:msg inView:view mode:YJProgressModeSuccess];
    [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    
}


/** 显示提示（1秒后消失,带Block） */
+(void)showMessage:(NSString *)msg inView:(UIView *)view compeleteBlock:(void (^)())compeleteBlock{
    [self show:msg inView:view mode:YJProgressModeSuccess];
    [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (compeleteBlock) {
            compeleteBlock();
        }
    });
}


+(void)showProgress:(NSString *)msg inView:(UIView *)view{
    [self show:msg inView:view mode:YJProgressModeLoading];
}

+(void)showMsgWithoutView:(NSString *)msg{
    UIWindow *view = [[UIApplication sharedApplication].windows lastObject];
    [self show:msg inView:view mode:YJProgressModeOnlyText];
    [[YJProgressHUD shareinstance].hud hideAnimated:YES afterDelay:1.0];
    
}

@end
